﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace DAL
{
    public class StyleDAL
    {
        /// <summary>
        /// 根据ID获取style详细信息
        /// </summary>
        /// <param name="styleid"></param>
        /// <returns></returns>
        public DataTable getStyle(string limit)
        {
            string cmdstr = "select * from Style where "+limit;
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
        }
        //添加样式
        public int AddStyle(Albon.Model.Style style)
        {
            SqlParameter[] parameters =new SqlParameter[] {
                    new SqlParameter("@ret",SqlDbType.Int),
					new SqlParameter("@stylename",style.styleName),
					new SqlParameter("@cnname",style.cnName),
					new SqlParameter("@styleImage", style.styleImage),
					new SqlParameter("@parentid", style.parentID),
					new SqlParameter("@active", style.active),
					new SqlParameter("@insertafter", style.insertAfter),
					new SqlParameter("@selectmode",style.selectMode),
					new SqlParameter("@buyclothmark",style.buyClothMark),
					new SqlParameter("@sex",style.sexID)
                    
            };
            parameters[0].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_styleadd", parameters);
            string str = parameters[0].Value.ToString();
            return int.Parse(parameters[0].Value.ToString());
        }
        //更新样式
        public void updateStyle(Albon.Model.Style style)
        {
            SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@ID",style.ID),
					new SqlParameter("@stylename",style.styleName),
					new SqlParameter("@cnname",style.cnName),
                    new SqlParameter("@selectmode",style.selectMode),
                    new SqlParameter("@sex",style.sexID),
					new SqlParameter("@styleImage", style.styleImage),
                    new SqlParameter("@buyclothmark",style.buyClothMark),
					new SqlParameter("@active", style.active),
					new SqlParameter("@insertafter", style.insertAfter)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_styleupdate", parameters);
        }

        //添加样式选择的规则(没有修改，如果需要修改某条数据的话删除该数据，然后再添加一条新的) 
        public void addSelectRule(string refID1, string refID2, string rule)
        {
            SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@refID1",refID1),
					new SqlParameter("@refID2",refID2),
					new SqlParameter("@rule",rule)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_selectruleadd", parameters);
        }
        //获取样式表中的规则
        //Co_Exist 选A则必选B,选了B，不一定先A
        //Un-Exist 选了A则不能选B,选了B也不能选A,取消A时，B项要显示 
        public DataSet getRule(string idlist)
        {
            StringBuilder cmdstr = new StringBuilder();
            cmdstr.Append("select refID2 as refID,parent2 as parent,[rule] from SelectRulesView where refID1 in ("+idlist+")");
            cmdstr.Append("select refID1 as refID,parent1 as parent,[rule] from SelectRulesView where [rule]=0 and refID2 in ("+idlist+")");
            return SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr.ToString(), null);
        }
        //删除样式规则
        public void deleteRule(string id)
        {
            string cmdstr = "delete from SelectRules where ID="+id;
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }
    }
}
